<template>
  <div>
    <van-nav-bar
         left-arrow
        class="app-nav-bar"
        title="快捷请假申请填写"
        @click-left="$router.back()"
      />
      <van-form @submit="onSubmit">
           <van-field
                v-model="form.studentNo"
                label="学号"
                :rules="[{ required: true, message: '请填写学号' }]"
            />
            <van-field
                v-model="classContent"
                right-icon="arrow"
                label="班级"
                placeholder="请选择班级"
                @click="classListShare=true"
            />
            <van-action-sheet 
                v-model="classListShare"
                title="请选择"
                :actions="classList" 
                @select="onSelect1" 
            />
            <van-field
                v-model="form.studentName"
                label="学生姓名"
                :rules="[{ required: true, message: '请填写姓名' }]"
            />
             <van-field
             readonly
                v-model="statusContent"
                right-icon="arrow"
                label="请假类型"
                placeholder="请选择请假类型"
                @click="statusListShare=true"
            />
            <van-action-sheet 
                v-model="statusListShare"
                title="请选择"
                :actions="status" 
                @select="onSelect2" 
            />
            <van-field
             readonly
                v-model="currentDate"
                right-icon="arrow"
                label="请假日期"
                placeholder="请选择请假日期"
                 @click="showPopup"
            />
            <van-popup v-model="dateShare" position="bottom" :style="{ height: '45%' }">
                <van-datetime-picker
                    type="date"
                    title="选择年月日"
                    :min-date="minDate"
                    :max-date="maxDate"
                    @confirm="confirm"
                    @cancel="dateShare = false"
                    :formatter="formatter"
                />
            </van-popup>
             <van-field
                v-model="form.leaveReason"
                label="请假原因"
                :rules="[{ required: true, message: '请填写请假原因' }]"
            />
            <div style="margin:16px">
                <van-button class="btncolor" round block type="info" native-type="submit">提交</van-button>
            </div>
        </van-form>
  </div>
</template>

<script>
import {Notify} from 'vant'
export default {
    name:'TeaApply',
    data(){
        return{
            minDate: new Date(2000, 0, 1),
            maxDate: new Date(2021, 10, 1),
            currentDate:'',
           form:{
               studentNo: '',
                classId: '',
                className: "",
                studentName: '',
                leaveType: '',
                leaveReason: ''
           },
           tableData:[],
            status:[],
            statusListShare:false,
            statusContent:"",
            classList:[],
            classListShare:false,
            classContent:"",
             dateShare:false,
             yyyy:null,
             MM:null,
             dd:null
        }
    },
     methods:{
         showPopup() {
            this.dateShare = true;
        },
         confirm(){
             this.currentDate=`${this.yyyy}-${this.MM}-${this.dd}`
            this.dateShare=false
         },
        async onSubmit(){
            var {data:res} = await this.request({
                url:this.$location.saveLeaveTea,
                method:'post',
                data:{
                    classId: this.form.classId,
                    className: "",
                    leaveDate: this.currentDate,
                    leaveReason: this.form.leaveReason,
                    leaveStatus: "",
                    leaveType: this.form.leaveType,
                    studentName: this.form.studentName,
                    studentNo: this.form.studentNo
                }
            })
            console.log(res)
            if(res.status==200){
                 this.$toast.success(res.msg)
                 this.$router.push('/Leave');
            }else{
                Notify({ type: 'danger', message: res.msg });
            }
        },
        onSelect2(item){
            this.statusContent=item.name;
            this.form.leaveType=item.value
            this.statusListShare = false;
        },
        async getStatusListAll(){
            const {data:res} =await this.request({url:this.$location.getleaveType});
            res.data.forEach(item=>{
                this.status.push({
                    value:item.dictValue,
                    name:item.dictLabel
                })
            })
        },
        onSelect1(item){
            this.classContent=item.name;
            this.form.classId=item.id
            this.classListShare = false;
        },
        async getClassListAll(){
            const {data:res} =await this.request({url:this.$location.getClassListAll});
            res.data.forEach(item=>{
                this.classList.push({
                    id:item.id,
                    name:item.className
                })
            })
        },
        formatter(type, val) {
            if(type === 'year'){
                this.yyyy=val;
                return `${val}年`;
            }else if (type === 'month') {
                 this.MM=val;
                return `${val}月`;
            } else if (type === 'day') {
                 this.dd=val;
                return `${val}日`;
            }
            return val;
        },
    },
    created(){
         this.getStatusListAll();
         this.getClassListAll()
    }
}
</script>

<style lang="less" scoped>
 /deep/ .app-nav-bar{
        background-color: #35d299;
        .van-nav-bar__title{
            color: #fff;
        }
        .van-icon{
            color: #fff;
        }
    }
    // .elin{
    //     .el-input {
    //     position: relative;
    //     font-size: 14px;
    //     display: inline-block;
    //     width: 72%;
    //      }
    // }
</style>